package com.example.demo.Aop;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;


@Aspect
@Component
public class AopLog {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Around("execution(public * com.example.demo.controller.*.*(..))")
    public Object doLogging(ProceedingJoinPoint pjp) throws Throwable{
        Long startTime=System.currentTimeMillis();
        ServletRequestAttributes requestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=requestAttributes.getRequest();
        logger.info(request.getMethod()+" "+request.getRequestURI());
        Object s=pjp.proceed();
        logger.info("cost:"+(System.currentTimeMillis()-startTime));
        return s;
    }
}
